<?php
   class Shipajax extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper("url");
		   $this->load->model("model_cruise");
		   $this->load->library("session");
		   $this->load->library("cart");
	   }
	   public function charter(){
	   		$numbernight=$this->input->post("numbernight");
			$start=$this->input->post("start");
			$startdate=date("Y-m-d",strtotime($start));
			$ship_id = $this->input->post("ship");
			$type   = "usd";//USD or VND
			$this->cart->destroy();
			$_SESSION['rate'] = $type;
			$data = $this->model_cruise->getrooms_ajax($ship_id,'','');
			$numb = ($numbernight - 1);
			foreach($data as &$cabin){
				$cabin["price"]=$this->model_cruise->getpricescharter($cabin['room_id'],$numb,$startdate);
			}
			$dataship['result'] = $this->model_cruise->getdata($ship_id);
			/*echo "<pre>";
			print_r($data[0]['price']);
			echo "</pre>";
			die();*/
			if($data[0]['price'] != NULL){
				$dataship['status'] = "Availability";
			}else{
				$dataship['status'] = "Not Availability";
			}
			$dataship["listcabin"]=$data;
			
			$this->load->view("checkrate/charter",$dataship);
	   }
	   public function index(){
		   $tour 	= $this->input->post("tour");
		   $adults 	= $this->input->post("adults");
		   $child 	= $this->input->post("child");
		   $infants = $this->input->post("infants");
		   $start   = strtotime($this->input->post("start"));
		   $ship_id = $this->input->post("ship");
		   $type   = $this->input->post("type");
		   $empty  = $this->input->post("empty");
		   if($empty == "true"){
			   $this->cart->destroy();
		   }
		   $this->cart->destroy();
		   $_SESSION['rate'] = $type;
		   $end_date = strtotime($this->findday($this->input->post("start"),($tour-1)));
		   $startdate = date("Y-m-d",$start);
		   $endate = date("Y-m-d",$end_date);
		   $data = $this->model_cruise->getrooms_ajax($ship_id,$start,$end_date);
		   $period = array();
		   //die($startdate." - ".$endate);
		   foreach($data as $key => $value){
			   $period[] = $this->model_cruise->getprices($value['room_id'],$tour,$startdate,$endate);
		   }
		   //$this->debug($period);
		   foreach($period as $k => $v){
			   if($period[$k] == NULL){
				   unset($data[$k]);
			   }
 			   $data[$k]['option'] = $period[$k];
			   if($data[$k]['option'] == NULL){
				    unset($data[$k]);
			   }
		   }
		   $status = "Availability";
		   //$this->debug($data);
		   echo '<table width="920" border="0" cellspacing="0" cellpadding="0">
					<tr>
					 	<td width="180" class="title">CABIN TYPE</td>
						<td width="65" class="title">SINGLE CABIN</td>
				        <td width="65" class="title">DOUBLE CABIN</td>
				        <td width="65" class="title">TWIN CABIN</td>
						<td width="65" class="title">ADULTS</td>
						<td width="65" class="title">CHILDREN</td>
						<td width="65" class="title">INFANTS</td>
						<td width="65" class="title">TOTAL PRICE</td>
						<td width="50" class="title">BOOK</td>
					</tr>';
			if($data != NULL){
				$stt = 0;
				foreach($data as $items){
				   $stt++;
				    $avaible = $items['option']['numberavai'];
					$price_single = $items['option']['pricesingle'];
					$price_double = $items['option']['pricedouble'];
					$price_child  = $items['option']['pricechild'];
					$price_infant = $items['option']['priceinfant'];
					$price_rate_single = $items['option']['priceold'];
					$price_rate_double = $items['option']['priceold'];	
				   if($type == "vnd"){
					   $rate = "0 VND";
					   $price_single = $price_single*21000;
					   $price_double = $price_double*21000;
					   $price_child  = $price_child*21000;
					   $price_infant = $price_infant*21000;
					   $price_rate_single = $price_rate_single*21000;
					   $price_rate_double = $price_rate_double*21000;
				   }else{
					   $rate = "$0";
				   }
				   if($items['room_icons'] != NULL){
						$icons = unserialize($items['room_icons']);
					}else{
						$icons = array("room_size" => "","room_air" => "","room_stand" => "","room_tivi" => "","room_jac" => "","room_bal" => "","room_ter" => "");
					}
					echo "<tr>";	
						echo "<td class='yellow' style='text-align:left;padding:10px 0px 10px 5px'><p style='font-weight:bold;padding:0px;margin:0px;font-size:13px' class='room_title$stt'>".$items['room_title']."</p><span class='idroom$stt' style='display:none'>".$items['room_id']."</span>";
						echo '<img onmouseover="showDetailImage(this,'."'".base_url()."uploads/rooms/".$items['room_image']."'".');" src="'.base_url()."uploads/rooms/".$items['room_image'].'" width="115" height="90px" style="margin:10px 0px 0px 0px;float:left" /><input type="text" id="img_aj'.$stt.'" value='.$items["room_image"].' style="display:none" />';
						echo "<ul style='list-style-type:none;padding-left:3px'>";
							if($icons['room_size'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_size']."' />Size : ".$items['room_cabin']."</li>";}
							if($icons['room_air'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_air']."' />Aircon</li>";}
							if($icons['room_stand'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_stand']."' /> Standing Shower</li>";}
							if($icons['room_tivi'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_tivi']."' />Tivi</li>";}
							if($icons['room_jac'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_jac']."' />Jacuzzi</li>";}
							if($icons['room_bal'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_bal']."' />Balcony</li>";}
							if($icons['room_ter'] != NULL){ echo "<li><img src='".base_url()."public/images/icons/".$icons['room_ter']."' />Terrace</li>";}
						echo"</ul>";
						echo "<div class='cls' style='padding-top:20px'></div>";
						echo "<div class='rleft'><a href='javascript:void(0)' name='".$stt."' class='arinfo' style='text-decoration:underline'>Room info</a><div style='display:none' class='divrinfo".$stt." roomsv' id='divrinfo".$stt."'><div class='roomimg'><img src='".base_url()."uploads/rooms/".$items['room_image']."' width='140' /><h3 style='text-align:center'>".$items['room_title']."</h3></div><div class='roominfo'><span class='close'>Close x</span>".$items['room_info']."</div></div></div>";
						echo "<div class='rright'><a href='javascript:void(0)' name='".$stt."' style='text-decoration:underline' class='acondition'>".$items['room_conditions']."</a><div class='divcondition".$stt." roomcon' id='divcondition".$stt."' style='display:none' >".$items['room_conditions_val']."</div></div>";
						echo "</td>";
						echo " <td>";
							echo "<input type='text' readonly='readonly' id='vat$stt' value='".$items['room_vat']."' size='3' style='display:none' />";
							echo "<input type='text' readonly='readonly' id='numavai$stt' value='".$avaible."' size='3' style='display:none' />";
							echo "<input type='text' readonly='readonly' id='typerate' value='".$type."' size='3' style='display:none' />";
							echo "<select name='singler$stt' id='singler$stt' onchange='price($stt)'>";
								  echo "<option value='0' selected='selected'>0</option>";
                                  for($i=1;$i<=$avaible;$i++){
									  echo "<option value='$i'>$i</option>";
								  }
				            echo "</select>";
						echo "</td>";
						echo " <td>";
							echo "<select name='doubler$stt' id='doubler$stt' onchange='price($stt)'>";
								  echo "<option value='0' selected='selected'>0</option>";
                                  for($j=1;$j<=$avaible;$j++){
									  echo "<option value='$j'>$j</option>";
								  }
				            echo "</select>";
						echo "</td>";
						echo " <td>";
							echo "<select name='twinr$stt' id='twinr$stt' onchange='price($stt)'>";
								 echo "<option value='0' selected='selected'>0</option>";
                                 for($k=1;$k<=$avaible;$k++){
								     echo "<option value='$k'>$k</option>";
								 }
				            echo "</select>";
						echo "</td>";
						echo " <td>";
							echo "<select name='adults$stt' id='adults$stt' onchange='price($stt)'>";
							for($i=0;$i<=($avaible*2);$i++){
								echo "<option value='$i'";
								if($i == $adults){ echo "selected='selected'";}
								echo ">$i</option>";
							}
				            echo "</select>";
							echo "<a id='price_single$stt' name='".$price_single."' style='display:none'></a>";
							echo "<a id='price_double$stt' name='".$price_double."' style='display:none'></a>";
							echo "<a id='price_promo_1sing$stt' name='".$price_rate_single."' style='display:none'></a>";
							echo "<a id='price_promo_2sing$stt' name='".$price_single."' style='display:none'></a>";
							echo "<a id='price_promo_1double$stt' name='".$price_rate_double."' style='display:none'></a>";
							echo "<a id='price_promo_2double$stt' name='".$price_double."' style='display:none'></a>";
						echo "</td>";
						echo " <td>";
							echo "<select name='child$stt' id='child$stt' onchange='price($stt)'>";
                            for($j=0;$j<=$avaible;$j++){
								echo "<option value='$j'";
								if($j == $child){ echo "selected='selected'";}
								echo ">$j</option>";
							}
				            echo "</select>";
							echo "<a id='price_child$stt' name='".$price_child."' style='display:none'></a>";
						echo "</td>";
						echo " <td>";
							echo "<select name='infant$stt' id='infant$stt' onchange='price($stt)'>";
                            for($k=0;$k<=$avaible;$k++){
								echo "<option value='$k'";
								if($k == $infants){ echo "selected='selected'";}
								echo ">$k</option>";
							}   	
				            echo "</select>";
							echo "<a id='price_infant$stt' name='".$price_infant."' style='display:none'></a>";
						echo "</td>";
						echo "<td>
						    <p><span id='line$stt' style='text-decoration:line-through'></span><span id='p$stt' style='color:#F90;font-size:13px;font-weight:bold;margin-top:3px;'></span></p>
							<p><span id='totalp$stt' style='color:#F90'>".$rate."</span></p>
							<p style='display:none'><span id='totalps$stt' style='color:#F90'>$0</span></p>
							</td>";
						echo "<td><p style='margin-bottom:5px;'><span class='okie$stt'></span></p><a href='#' id='".$stt."' class='arate'>Select</a></td>";
					echo "</tr>";
				}
				echo "<tr>";
					echo '<td height="40" colspan="10" style="text-align:left;font-size: 12px;" valign="top">';
                    echo "<div style='float:right;'>";
					?>
                    <a href="<?php echo base_url(); ?>your-cart#infocruise" class="continus" onclick="alert('Press SELECT BUTTON to select cabin!')">CONTINUE</a>
                    <?php
                    echo "</div>";
                    echo "<h3 id='h3bao'>Notice:</h3>";
                    echo "<table cellspacing='0' cellpadding ='0' border='0' id='tbao' style='border: none;'>";
                    echo "<tr>";
                        echo "<td width='140px;'>";
                            echo "<img id='inf' src='".base_url()."public/images/child.png' alt='infant'/>";
                            echo "Infant 0-4 year(s)";
                        echo "</td>";
                        echo "<td>";
                            echo "Stay for free if using existing bedding. Note, if you need a cot there may be an extra charge.";
                        echo "</td>";
                    echo "</tr>";
                    echo "<tr>";
                        echo "<td width='140px;'>";
                        echo "<img id='chi' src='".base_url()."public/images/per.png' alt='chidren'/>";
                        echo "Children 5-11 year(s)";
                        echo "</td>";
                        echo "<td>";
                        echo "Must stay with parents";
                        echo "</td>";
                    echo "</tr>";
                    echo "</table>";
                    echo "
                        <ul id='ulbao' style='float: left;margin-left: 23px;margin-bottom: 10px;'>
                            <li>
                               Double Cabin can accommodate a maximum of 2 adults, 1 child and 1 infant.
                            </li>
                            <li>
                          Single Cabin can accommodate a maximum of 1 adult.
                            </li>
							<li>
							Guests over 11 years old are considered as adults.
							</li>
                        </ul>
                    ";
				echo "</tr>";
			}else{
				echo "<tr><td colspan='10' height='100'>No room available</td></tr>";
			}
			echo "</table>";
	   }
	   function findday($orgDate,$date){
	      $cd = strtotime($orgDate);
	      $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$cd),date('d',$cd) + $date,date('Y',$cd)));
	      return $retDAY;
	  }
   }
